/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poo.biblioteca.vistas.libros;

import biblioteca.entidades.Editorial;
import biblioteca.entidades.Genero;
import biblioteca.entidades.Libro;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import poo.biblioteca.vistas.IfrBase;

/**
 *
 * @author apo
 */
public class IfrMnttoLibro extends IfrBase {

    /**
     * Creates new form IfrMnttoLibro
     */
    public IfrMnttoLibro() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlCriterios = new javax.swing.JPanel();
        chkISBN = new javax.swing.JCheckBox();
        chkTitulo = new javax.swing.JCheckBox();
        chkEditorial = new javax.swing.JCheckBox();
        chkGenero = new javax.swing.JCheckBox();
        txtISBN = new javax.swing.JTextField();
        txtTitulo = new javax.swing.JTextField();
        cmbEditorial = new javax.swing.JComboBox();
        cmbGenero = new javax.swing.JComboBox();
        btnBuscar = new javax.swing.JButton();
        scrMnttoLibros = new javax.swing.JScrollPane();
        tblResultadoBusqueda = new javax.swing.JTable();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Mantenimiento de Libros");

        pnlCriterios.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de Búsqueda"));

        chkISBN.setText("ISBN:");

        chkTitulo.setText("Título:");

        chkEditorial.setText("Editorial:");

        chkGenero.setText("Género:");

        btnBuscar.setMnemonic('B');
        btnBuscar.setText("Buscar");

        javax.swing.GroupLayout pnlCriteriosLayout = new javax.swing.GroupLayout(pnlCriterios);
        pnlCriterios.setLayout(pnlCriteriosLayout);
        pnlCriteriosLayout.setHorizontalGroup(
            pnlCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCriteriosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnlCriteriosLayout.createSequentialGroup()
                        .addGroup(pnlCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chkISBN)
                            .addComponent(chkTitulo)
                            .addComponent(chkEditorial)
                            .addComponent(chkGenero))
                        .addGap(12, 12, 12)
                        .addGroup(pnlCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                            .addComponent(cmbGenero, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbEditorial, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtISBN, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        pnlCriteriosLayout.setVerticalGroup(
            pnlCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCriteriosLayout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addGroup(pnlCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkISBN)
                    .addComponent(txtISBN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkTitulo)
                    .addComponent(txtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkEditorial)
                    .addComponent(cmbEditorial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkGenero)
                    .addComponent(cmbGenero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnBuscar))
        );

        scrMnttoLibros.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultados de Búsqueda"));

        tblResultadoBusqueda.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        scrMnttoLibros.setViewportView(tblResultadoBusqueda);

        btnModificar.setMnemonic('M');
        btnModificar.setText("Modificar");
        btnModificar.setEnabled(false);

        btnEliminar.setMnemonic('E');
        btnEliminar.setText("Eliminar");
        btnEliminar.setEnabled(false);

        btnCancelar.setMnemonic('C');
        btnCancelar.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlCriterios, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(scrMnttoLibros, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnModificar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrMnttoLibros, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnModificar)
                    .addComponent(btnEliminar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JCheckBox chkEditorial;
    private javax.swing.JCheckBox chkGenero;
    private javax.swing.JCheckBox chkISBN;
    private javax.swing.JCheckBox chkTitulo;
    private javax.swing.JComboBox cmbEditorial;
    private javax.swing.JComboBox cmbGenero;
    private javax.swing.JPanel pnlCriterios;
    private javax.swing.JScrollPane scrMnttoLibros;
    private javax.swing.JTable tblResultadoBusqueda;
    private javax.swing.JTextField txtISBN;
    private javax.swing.JTextField txtTitulo;
    // End of variables declaration//GEN-END:variables

    public void setEditoriales(ArrayList<Editorial> editoriales) {
        for (Editorial editorial : editoriales) {
            cmbEditorial.addItem(editorial);
        }
    }

    public void setGeneros(ArrayList<Genero> generos) {
        for (Genero genero : generos) {
            cmbGenero.addItem(genero);
        }
    }

    public void btnCancelarAddActionListener(ActionListener actionListener) {
        btnCancelar.addActionListener(actionListener);
    }

    public void btnBuscarAddActionListener(ActionListener actionListener) {
        btnBuscar.addActionListener(actionListener);
    }

    public String getISBN() {
        if (chkISBN.isSelected()) {
            return txtISBN.getText();
        } else {
            return null;
        }
    }

    public String getTitulo() {
        if (chkTitulo.isSelected()) {
            return txtTitulo.getText();
        } else {
            return null;
        }
    }

    public Editorial getEditorial() {
        if (chkEditorial.isSelected()) {
            return (Editorial) cmbEditorial.getSelectedItem();
        } else {
            return null;
        }
    }

    public Genero getGenero() {
        if (chkGenero.isSelected()) {
            return (Genero) cmbGenero.getSelectedItem();
        } else {
            return null;
        }
    }

    public void setResultadoBusqueda(ArrayList<Libro> libros) {
        tblResultadoBusqueda.setModel(new TbmLibro(libros));
    }

    public void tblLibroAddMouseListener(MouseAdapter mouseAdapter) {
        tblResultadoBusqueda.addMouseListener(mouseAdapter);
    }

    public int getFilaSeleccionada() {
        return tblResultadoBusqueda.getSelectedRow();
    }

    public void permitirEdicion(boolean permitir) {
        btnEliminar.setEnabled(permitir);
        btnModificar.setEnabled(permitir);
    }

    public void btnEliminarAddActionListener(ActionListener actionListener) {
        btnEliminar.addActionListener(actionListener);
    }

    public Libro getLibroSeleccionado() {
        int filaSeleccionada = tblResultadoBusqueda.getSelectedRow();
        if (filaSeleccionada < 0) {
            return null;
        }
        Libro libroSeleccionado = ((TbmLibro) tblResultadoBusqueda.getModel()).getLibroEnFila(filaSeleccionada);

        return libroSeleccionado;
    }

    public boolean confirmarEliminacion() {
         return (JOptionPane.showConfirmDialog(this, "¿Está seguro de eliminar "
                + "el Libro seleccionado?", "Confirmación",
                JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION
                ? true : false);
    }

    public void eliminarLibroSeleccionado() {
        ((TbmLibro)tblResultadoBusqueda.getModel())
                .eliminar(tblResultadoBusqueda.getSelectedRow());
    }

    public void deseleccionarTabla() {
        tblResultadoBusqueda.clearSelection();
        permitirEdicion(false);
    }

    public void btnModificarAddActionListener(ActionListener actionListener) {
        btnModificar.addActionListener(actionListener);
    }
}
